#include <boost/algorithm/string.hpp>
#include "BodyPart.h"
#include "../../smtp/data/SMTPDefaults.h"
#include "../core/_8BitConverter.h"

namespace ba = boost::algorithm;

using namespace kc_sf;

// Konstruktory ----------------------------------------------------- //

BodyPart::BodyPart(const std::string &text)
: b_original_text(text),
  b_encoding (_8Bit),
  b_charset (ASCII)
{
	ba::trim_if(b_original_text, ba::is_any_of( WSP ));
	b_size = text.size();
	b_decoded_text = "";
}

BodyPart::BodyPart( const std::string &text, 
            EncodingType encoding,
            const std::string &charset)
: b_original_text (text),
  b_encoding (encoding),
  b_charset (charset)
{
	ba::trim_if(b_original_text, ba::is_any_of( WSP ));
	b_size = text.size();
	b_decoded_text = "";
}

// ------------------------------------------------------------------ //

UnicodeString BodyPart::getDecodedText()
{
	if (b_decoded_text.length() == 0)
	{
		_8BitConverter conv(b_encoding, b_charset);
		conv.decode(b_original_text, b_decoded_text);
	}

	return b_decoded_text;
}

const std::string & BodyPart::getOriginalText() const
{
	return b_original_text;
}

unsigned long BodyPart::getSize() const
{
	return b_size;
}
